package com.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.constant.Constant;
import com.web.domain.CartItem;
import com.web.domain.Order;
import com.web.domain.OrderItem;
import com.web.domain.User;
import com.web.service.OrderService;
import com.web.service.ProductService;
import com.web.utils.BeanFactory;
import com.web.utils.UUIDUtils;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/order") 
public class OrderServlet extends CartServlet {
	private static final long serialVersionUID = 1L;
	
	private OrderService os = (OrderService) BeanFactory.getBean("OrderService");

	public String getOrder(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		getData(request, response);
		String oid = UUIDUtils.getId();
		request.setAttribute("oid", oid);
		return "/jsp/order_info.jsp";
	}

	/*
	 * 保存订单到数据库
	 */
	public String save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接收页面传来数据
		try {
			request.setCharacterEncoding("UTF-8");
			ProductService ps = (ProductService) BeanFactory.getBean("ProductService");
			String address = request.getParameter("address");
			String name = request.getParameter("name");
			String telephone = request.getParameter("telephone");
			String oid=request.getParameter("oid");
			Order order = new Order();
			// 随机id
			order.setOid(oid);
			// 地址
			order.setAddress(address);
			// 设置时间
			Date date = new Date();
			SimpleDateFormat temp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String date2 = temp.format(date);
			Date date3 = temp.parse(date2);
			order.setOrdertime(date3);
			// 联系方式
			order.setTelephone(telephone);
			// 用户
			
			User user = (User) request.getSession().getAttribute("user");
			String activeUser = user.getUid();
			order.setUid(activeUser);
			// 设置状态
			order.setState(Constant.ORDER_YIFUKUAN);

			List<CartItem> cartF = cs.getFromCart(activeUser);
			double total = 0.0;
			for (CartItem cartItem2 : cartF) {
				OrderItem items = new OrderItem();
				String productId = cartItem2.getCt_pid();
				items.setItemid(UUIDUtils.getId());
				cartItem2.setProduct(ps.getById(productId));
				total += cartItem2.getSubtotal();
				items.setSubtotal(cartItem2.getSubtotal());
				items.setCount(cartItem2.getCt_count());
				items.setProduct(cartItem2.getProduct());
				items.setOrder(order);
				order.getItems().add(items);
			}
			// 总价
			order.setTotal(total);
			// 收货人
			order.setName(name);
			// 保存到数据库
			os.save(order);

			cs.deleteAll(activeUser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return showOrder(request, response);
	}

	/*
	 * 查询订单信息
	 */
	public String showOrder(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			String activeUser;
			User user = (User) request.getSession().getAttribute("user");
			if (user != null) {
				activeUser = user.getUid();
			} else {
				return "/UserService?method=loginUI";
			}
			List<Order> order = os.findAll(activeUser);
			request.setAttribute("o", order);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/jsp/order_list.jsp";
	}

	

	public String callback(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try {
				String oid = request.getParameter("oid");
				// 修改订单状态
				OrderService s = (OrderService) BeanFactory.getBean("OrderService");
				Order order = s.getOrderByOid(oid);
				order.setState(Constant.ORDER_YIFUKUAN);
				s.updateOrder(order);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return showOrder(request, response);
	}
}
